from django.db import models
from django.conf import settings
from django.contrib.auth import get_user_model

User = get_user_model()


class Dingding(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, editable=False, verbose_name="添加用户")
    name = models.CharField(max_length=127, verbose_name="名称", help_text="建议使用钉钉群名+机器人名")
    desc = models.CharField(max_length=1024, verbose_name="备注")
    access_token = models.CharField(max_length=127, verbose_name="Access Token")
    secret = models.CharField(max_length=127, verbose_name="Secret")

    enable = models.BooleanField(default=True, verbose_name="启用")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural = "钉钉配置"


class LiveMonitoringPolicy(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, editable=False, verbose_name="添加用户")
    name = models.CharField(max_length=64, verbose_name="策略名称")
    desc = models.CharField(default="", max_length=1024, verbose_name="描述")
    amplitude_alert_threshold = models.FloatField(default=1.5, verbose_name="振幅告警阈值")
    amplitude_alert_time_window = models.IntegerField(default=60 * 10, verbose_name="振幅告警时间窗口")

    @property
    def cache_length(self) -> int:
        length = self.amplitude_alert_time_window // settings.COLLECT_INTERVAL
        return length

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural = "监控策略"


class LiveStock(models.Model):
    """需要盯盘的股票池"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, editable=False, verbose_name="添加用户")
    code = models.CharField(max_length=8, verbose_name="代码", db_index=True)
    name = models.CharField(max_length=4, verbose_name="名称")
    monitoring_policy = models.ForeignKey(to=LiveMonitoringPolicy, on_delete=models.PROTECT, verbose_name="监控策略")

    enable = models.BooleanField(default=True, verbose_name="启用")

    def __str__(self):
        return f"{self.name} {self.code}"

    class Meta:
        verbose_name = verbose_name_plural = "盯盘股票"


class BackupStock(models.Model):
    advice_choices = [
        ("大胆买入", "大胆买入"),
        ("谨慎买入", "谨慎买入"),
        ("谨慎卖出", "谨慎卖出"),
        ("大胆卖出", "大胆卖出"),
    ]
    code = models.CharField(max_length=8, verbose_name="代码", db_index=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, editable=False, verbose_name="添加用户")
    name = models.CharField(max_length=4, verbose_name="名称", editable=False)
    max = models.FloatField(default=999999, verbose_name="价格上限")
    min = models.FloatField(default=0, verbose_name="价格下限")
    advice = models.CharField(max_length=127, choices=advice_choices, default=advice_choices[0][0], verbose_name="建议")
    message = models.CharField(max_length=1024, verbose_name="留言")

    current_price = models.FloatField(default=0, verbose_name="当前价格", editable=False)
    collect_datetime = models.DateTimeField(null=True, verbose_name="信息更新时间", editable=False)

    def is_ready(self):
        return self.min < self.current_price < self.max

    def __str__(self):
        return f"{self.name} {self.code}"

    class Meta:
        verbose_name = verbose_name_plural = "备选股票"
